import GameData_red_blue_man, {emitType} from "./GameData_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import ControlScript from "../PublicScript/ControlScript";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GetSkin extends cc.Component {

    @property(sp.Skeleton)
    red: sp.Skeleton = null;
    @property(sp.Skeleton)
    blue: sp.Skeleton = null;
    @property({tooltip: "对应的皮肤编号"})
    skinNum: number = 0 ;
    @property({tooltip: "title", type: cc.Sprite})
    title: cc.Sprite = null;
    @property({tooltip: "标题替换的图片", type: cc.SpriteFrame})
    titleSpriteFrame: cc.SpriteFrame[] = [];
    @property(cc.Node)
    btnLayout: cc.Node = null;
    @property({displayName:"需要看视频的次数"})
    videoCount: number = 0;
    @property(cc.Label)
    videoLabel: cc.Label = null;
    @property(cc.Node)
    videoBtnNode: cc.Node = null;

    titleSpriteCount = 0;
    //保存对应皮肤下标
    skinIndex = 0;

    onLoad () {

    }

    start () {
        this.init();
    }

    setInitData(skinNum, titleSpriteCount, showBtn?, videoCount?, skinIndex?){
        this.skinNum = skinNum;
        this.titleSpriteCount = titleSpriteCount;
        this.btnLayout.active = false;
        if(showBtn){
            this.btnLayout.active = true;
            this.videoCount = videoCount;
            this.skinIndex = skinIndex;
            this.eggInit();
        }
    }

    eggInit(){
        if(GameData_red_blue_man.eggCount[this.skinIndex] >= this.videoCount){
            this.videoBtnNode.children[0].active = false;
            this.videoBtnNode.children[1].active = true;
            cc.game.emit(emitType.getEggSkin);
            GameData_red_blue_man.skinData[this.skinIndex].unLock = 1;
            GameData_red_blue_man.saveSkinData();
            ControlScript.createToast("已经获得了该皮肤,快去商店查看");
            return;
        }
        this.videoLabel.string =  GameData_red_blue_man.eggCount[this.skinIndex] + ";" + this.videoCount;
    }

    protected onDestroy(): void {
        cc.game.emit(emitType.eggSkinPage);
    }

    onClick(){
        if(GameData_red_blue_man.eggCount[this.skinIndex] >= this.videoCount){
            ControlScript.createToast("已经获得了该皮肤");
            return;
        }
        Music.GamePlayEffect(musicName.click);
        GameData_red_blue_man.showVideo({callback:()=>{
                GameData_red_blue_man.eggCount[this.skinIndex] += 1;
                GameData_red_blue_man.saveEggCount();
                this.eggInit();
            },name: "彩蛋界面获取皮肤"})
    }


    init(){
        this.red.setSkin("red"+this.skinNum);
        this.blue.setSkin("blue"+this.skinNum);
        this.title.spriteFrame = this.titleSpriteFrame[this.titleSpriteCount];
    }

    // update (dt) {}
}
